﻿using System;
using TradeMeSDK;

namespace TradeMo
{
    public enum SearchType
    {
        General,
        UsedCars, 
        Boats, Bikes,
        Jobs,
        Residential, Rental, OpenHomes, Rural, Lifestyle, Retirement,
        Flatmates,
        Commercial, CommercialLease
    }

    public class SearchResultItem
    {
        public static SearchResultItem Build(ListedItemDetail listing)
        {
            return new SearchResultItem { Listing = listing };
        }

        public ListedItemDetail Listing { get; set; }

        public string DisplayPrice
        {
            get
            {
                if (Listing == null) return "";
                if (IsJob) return "";
                
                if (Listing.MaxBidAmount > 0) 
                    return string.Format("$ {0:0.00}", Listing.MaxBidAmount);

                else if (!string.IsNullOrEmpty(Listing.PriceDisplay)) 
                    return Listing.PriceDisplay;

                //else if (Listing.BuyNowPrice > 0) price = Listing.BuyNowPrice;

                else if (Listing.StartPrice > 0) 
                    return string.Format("$ {0:0.00}", Listing.StartPrice);

                return "";
            }
        }
        public string DisplayBidCount
        {
            get
            {
                if (Listing == null) return "";
                if (IsJob) return Listing.JobLocation;
                if (IsProperty) return "";
                return Listing.BidCount == 0 ? "no bids" : string.Format("{0} bids", Listing.BidCount);
            }
        }
        public string DisplayDate
        {
            get
            {
                if (Listing == null) return "";

                TimeSpan span = Listing.EndDate - DateTime.UtcNow;

				if (span.TotalHours > 24)
					return Listing.EndDate.ToLocalTime().ToString("ddd d MMMM, hh:mm tt");
				if (span.TotalHours > 1)
					return Listing.EndDate.ToLocalTime().ToString("t");
				if (span.TotalMinutes > 1)
					return (int)span.TotalMinutes + " mins";
				if (span.TotalSeconds > 1)
					return (int)span.TotalSeconds + " seconds";
				return "closed";
            }
        }

        public bool IsJob 
        { 
            get 
            {
                if (Listing == null) return false;
                if (string.IsNullOrEmpty(Listing.Category)) return false;
                return Listing.Category.StartsWith("5000-");
            } 
        }
        public bool IsProperty
        {
            get
            {
                if (Listing == null) return false;
                if (string.IsNullOrEmpty(Listing.Category)) return false;
                return Listing.Category.StartsWith("0350-");
            }
        }
        public bool IsMotor
        {
            get
            {
                if (Listing == null) return false;
                if (string.IsNullOrEmpty(Listing.Category)) return false;
                return Listing.Category.StartsWith("0001-");
            }
        }

        public string Message { get; set; }
    }
}